package com.smarthome.core.account.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShAccountFeatureService;
import com.smarthome.core.account.AccountFeatureService;
import com.smarthome.domain.bean.ShAccount;
import com.smarthome.domain.bean.ShAccountFeature;
import com.smarthome.domain.vo.boss.Tree;

@Service("accountFeatureService")
public class AccountFeatureServiceImpl implements AccountFeatureService {
	
	@Resource
	private ShAccountFeatureService shAccountFeatureService;
	
	@Override
	public List<ShAccountFeature> queryAllAccountFeatures() {
		return shAccountFeatureService.queryAllAccountFeatures();
	}

	@Override
	public List<ShAccountFeature> queryFeaturesByCondition(
			ShAccountFeature feature) {
		
		return shAccountFeatureService.selectEntryList(feature);
	}
	@Override
	public ShAccountFeature queryAccountFeatureById(Long id) {
		ShAccountFeature queryFeature=shAccountFeatureService.selectEntryById(id);
		if(queryFeature.getParentId()!=null){
			queryFeature.setParentFeature(shAccountFeatureService.selectEntryById(queryFeature.getParentId()));
		}
		return queryFeature;
	}

	@Override
	public List<ShAccountFeature> queryAccountFeaturesByAccountId(Long accountId) {
		return shAccountFeatureService.queryAccountFeaturesByAccountId(accountId);
	}

	@Override
	public List<Tree> tree(ShAccount account) {
		List<ShAccountFeature> currentFeatures=null;
		if(null == account){
			currentFeatures = queryAllAccountFeatures();
		}else{
			currentFeatures = queryAccountFeaturesByAccountId(account.getId());
		}
		return generFeatureTree(currentFeatures, 0L);
	}
	
	private List<Tree> generFeatureTree(List<ShAccountFeature> features,Long parentId){
		List<Tree> trs = new ArrayList<Tree>();
		for(ShAccountFeature feature:features){
			if(parentId == feature.getParentId()){
				Tree tr=new Tree();
				tr.setPid(parentId);
				tr.setText(feature.getName());
				tr.setId(feature.getId());
				tr.setChildren(generFeatureTree(features, feature.getId()));
				trs.add(tr);
			}
		}
		
		return trs;
	}

	@Override
	public List<ShAccountFeature> treeGrid(ShAccount account) {
		List<ShAccountFeature> currentFeatures=null;
		if(account == null){
			currentFeatures=queryAllAccountFeatures();
		}else{
			currentFeatures=queryAccountFeaturesByAccountId(account.getId());
		}
		
		List<ShAccountFeature> allFeatures=queryAllAccountFeatures();
		Map<Long,ShAccountFeature> rsMap=new HashMap<Long, ShAccountFeature>();
		for(ShAccountFeature rs:allFeatures){
			rsMap.put(rs.getId(), rs);
		}
		
		for(ShAccountFeature rs:currentFeatures){
			if(null != rs.getParentId()&& 0L != rs.getParentId()){
				rs.setParentFeature(rsMap.get(rs.getParentId()));
			}
		}
		return currentFeatures;
	}

	@Override
	public void add(ShAccountFeature feature) {
		shAccountFeatureService.saveOrUpdate(feature);
	}

	@Override
	public void edit(ShAccountFeature feature) {
		shAccountFeatureService.saveOrUpdate(feature);
	}

	@Override
	public void delete(Long id) {
		shAccountFeatureService.deleteByIds(id);
	}

}
